﻿/*
 * Calculate the age based on a birth date and a reference date.
 */
create function [Date].[CalculateAge] ( @p_BirthDate date, @p_ReferenceDate date )
returns int as
begin

  return DateDiff( Year, @p_BirthDate, @p_ReferenceDate ) - 
         ( Case When DateAdd( Year, DateDiff( Year, @p_BirthDate, @p_ReferenceDate ), @p_BirthDate ) > @p_ReferenceDate Then 1 Else 0 End )
  
end